;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var validate = require("validate");
	var css_collection = require("./commodityPriceCollection.css");
	var tools = require("tools");
	var html_add;
	var fileUuid;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var sTime;
	var eTime;
	var collector;
	var region;
	var collectionMaterialId;
	
	var init = function(num) {
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		$(".table-section").removeClass("none");
		$(".footer").removeClass("none");
		
		html_add = require("./commodityPriceCollection-add.html");
		inquireCollectionMaterialList();
		initEvent();
		router.init();
	};

	var initEvent = function() {
		
		//绑定点击事件
		$("#main").on("click", "#uploadPrice", function(){
			uploadPrice();
			$("#add_price_dialog").on("change", "#priceMaterial", function(){
				uploadFile();
			});
		});
		
		$("#main").on("click", "#priceCollectionSearch", function(){
			sTime = $("#sTime").val();
			eTime = $("#eTime").val();
			collector = $("#collector").val();
			region = $("#region").val();
			inquireCollectionMaterialList();
		});
		
		$("#main").on("click", ".delete", function(){
			var bulkstockCollectionMaterialId = $(this).parent().parent().parent().attr("bulkstockCollectionMaterialId");
			deleteCollectionMaterial(bulkstockCollectionMaterialId);
		});
	};
	
	var uploadPrice = function(){
		$("#popdialog").html(html_add);
		tools.loadSelectList($("#add_price_dialog select[name=dataSourceId]"), "inquireDataSourceList.json", "dataSourceId", "dataSource");
		dialog.showDialog($("#add_price_dialog"));
		$("#add_price_dialog").on("click", "#surePriceMaterial", function(){
			if(collectionMaterialId=$("#priceMaterial").val()==null){
				base.alert(2,"请选择文件");
			}else{
				createCollectionMaterial();
			}
		});
	};
	
	//新增大宗商品价格采集附件
	var createCollectionMaterial = function(){
        base.fnAjax({
            action: "createCollectionMaterial.json",
            oData: {
                fileUuid:fileUuid,
                dataSourceId: $("#add_price_dialog select[name=dataSourceId]").val()
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	dialog.hideDialog($("#add_price_dialog"));
                	router.route("commodityPriceCollection/commodityPriceCollection.html");
                } else {
                    base.alert(2,data.message);
                }
            }
        });
    };
    
     //获取货物类别
    var getCommodityCategory = function(){
		base.fnAjax({
			action: "inquireBulkstockCategoryPageList.json",
			async: false,
			oData: {
				activeState: true
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var tpl = $("#commodityPriceTpl").html();
                	var html = juicer(tpl, data);
                	$("#commodityPriceBody").html(html);
                } else {
//                  base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
	//获取大宗商品价格采集列表
    var inquireCollectionMaterialList = function(){
        base.fnAjax({
            action: "inquireCollectionMaterialPageList.json",
            oData: {
				collector:collector,
				region:region,
				endDate:eTime,
				startDate:sTime,
				pageIndex: pageIndex,
                pageSize: pageSize,
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var tpl = $("#priceCollectionTpl").html();
                	var html = juicer(tpl,data);
                	$("#priceCollectionbody").html(html);
                	totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl($("#CollectionPagerCon"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireCollectionMaterialList();
                        });
                     	sTime = "";
						eTime = "";
						collector = "";
						region = "";
                } else {
                    base.alert(2,data.message);
                }
            }
        });
    };
    
    //上传附件
	var uploadFile = function() {
		if ($("#priceMaterial").val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: 'priceMaterial',
				dataType: 'json',
				success: function(data) {
					fileUuid = data.data[0].fileUuid;
					$(".priceMaterial").html(data.data[0].fileName);
				},
				complete: function(xml) {
					$("#uploadImg").unbind().on('change', function() {
						uploadPicture();
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}
	};
    
    //删除大宗商品采集附件
    var deleteCollectionMaterial = function(bulkstockCollectionMaterialId){
        base.fnAjax({
            action: "disableCollectionMaterial.json",
            oData: {
                bulkstockCollectionMaterialId:bulkstockCollectionMaterialId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	base.openUrl("commodityCategory.html#commodityPriceCollection/commodityPriceCollection.html");
                } else {
                    base.alert(2,data.message);
                }
            }
        });
    };

	return {
		init: init
	};
});